#include <stdio.h>
#include <string.h>

#define BUFFER_SIZE 10


/* 找到数组的最小值 */
int findNumMinVal(int *nums, int numSize, int *pos)
{
    int adx = 0;
    int jdx = 1;
    for(int idx = 0; idx < numSize; idx++)
    {
        if(nums[idx] > nums[adx])
        {
            *pos = adx;
        }
        else
        {
            *pos = idx;
            adx = idx;  
        }
    }
    /* 返回最小值 */
    return nums[adx];
}


/* 交换器 */
int swapNum(int *val1, int *val2)
{
    int tempVal = *val1;
    *val1 = *val2;
    *val2 = tempVal;
    return 0;
}

/* 打印 */
int printfNums(int *nums, int numsSize)
{
    for(int idx = 0; idx < numsSize; idx++)
    {
        printf("%d\t", nums[idx]);
    }
    printf("\n");
}

/* 选择排序 */
/*
第一个默认排好序了
交换呢
插入排序是覆盖非交换
*/
int insertSort(int *nums, int numsSize)
{ 
    int copyNum = 0;
    //copyNum = nums[0];
    for(int idx = 1; idx < numsSize; idx++)
    {
        //copyNum = nums[idx];
        /* 索引备份 */
        int preIdx = idx - 1;
        copyNum = nums[idx];

        while(nums[preIdx] > copyNum && preIdx >= 0)
        {
            nums[preIdx + 1] = nums[preIdx];
            preIdx--;
        }
        nums[preIdx + 1] = copyNum;
    }    
    return 0;
}


int main()
{
    int pos = 0;
    int nums[] = {22, 6, 4, 2};
    /* 
    int nums[BUFFER_SIZE] = {22, 33, 44, 66};
    有了BUFFER_SIZE数组就硬性给你分配了100的空间，若没有BUFFER_SIZE，
    你有多少就分配多少，前提是你不超过BUFFER_SIZE
    */
    int numsSize = sizeof(nums) / sizeof(nums[0]);
    //int numsSize = strlen(nums);

    int minNum = findNumMinVal(nums, numsSize, &pos);
    printf("pos:%d\n", pos);
    printf("minnum:%d\n", minNum);
    printf("numsSiz:%d\n", numsSize); 

    insertSort(nums, numsSize);
    //selectSort02(nums, numsSize);
    printfNums(nums, numsSize);  
    
    return 0;
}
